// pages/recharge/recharge.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
      price:['50元','100元','200元','其它'],
      CheckPrice:0,
      balance: 0,
      inputPrice: 0
  },
  // 跳转余额明细
  balanceDetail() {
    wx.navigateTo({
      url: '../fineBalance/fineBalance'
    })
  },
  choosePrice:function(e){
    this.setData({
      CheckPrice: e.currentTarget.id
    })
  },
  chooseOther:function(){
    this.setData({
      CheckPrice:3
    })
  },
  rechartText: function() {
    wx.navigateTo({
      url: '../article/article?src=' + util.rootDocment + '/article/article/2&id=充值协议',
    })
  },
  // 获取输入的金额
  getPrice(e) {
    console.log(e);
    this.setData({
      inputPrice: parseFloat(e.detail.value)
    })
  },
  // 充值
  toRecharge() {
    var that = this;
    console.log(this.data.CheckPrice);
    var selectedNum = this.data.CheckPrice;
    var price = 0;
    if(selectedNum >= 0 && selectedNum <= 2) {
      price = parseFloat(this.data.price[selectedNum]);
    } else if(selectedNum >= 3) {
      price = parseFloat(this.data.inputPrice);
      price = price || 0;
    }
    console.log(price);
    if(price < 0.01 || price == '') {
      wx.showToast({
        title: '金额必须大于0',
        icon: 'none'
      })
    } else {
      util.req('/payment/recharge', { 'memberId': globalData.memberId, 'amount': price}, function(res) {
        if (res.resultCode == 200) {
          var resultContent = res.resultContent;
          console.log(res);
          wx.requestPayment({
            'timeStamp': resultContent.timeStamp + "",
            'nonceStr': resultContent.nonceStr + "",
            'package': resultContent.package,
            'signType': 'MD5',
            'paySign': resultContent.sign,
            'success': function (data) {
              console.log(data);
              var memberId = globalData.memberId;
              console.log(memberId);
              util.req('/member/detail', { 'memberId': memberId }, function (res) {
                console.log(res);
                // 格式化余额，保留小数点后两位
                res.resultContent.balance = (res.resultContent.balance).toString();
                if (res.resultContent.balance.indexOf('.') == -1) {
                  res.resultContent.balance = res.resultContent.balance + '.00';
                } else if (res.resultContent.balance.charAt(res.resultContent.balance.length - 2) == '.') {
                  res.resultContent.balance = res.resultContent.balance + '0'
                }
                globalData.userInfo = res.resultContent;
                that.setData({
                  balance: globalData.userInfo.balance
                })
              })
            }
          })
        } else {
          wx.showToast({
            title: res.resultMsg,
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(globalData);
    this.setData({
      balance: globalData.userInfo.balance
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  _jump(evt){
    util.jump(evt);
  }
})